package com.kq.tallybookl.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.kq.tallybookl.R;
import com.kq.tallybookl.database.FakeManager;
import com.kq.tallybookl.util.GetIMEI;
import com.kq.tallybookl.util.GetMD5;

public class MainActivity extends Activity {

    private SharedPreferences prefs;
    private SharedPreferences.Editor editer;

    private TextView textView_title;
    private EditText editText_password;
    private Button button_confirm;

    private FakeManager manager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();

        if(prefs.getBoolean(PreferencesActivity.FIRST_OPEN,true)){
            // 获取手机的IMEI号，并设置为假数据库密码
            String mIMEI = prefs.getString(PreferencesActivity.PHONE_IMEI,null);
            if(mIMEI == null) {
                mIMEI = GetIMEI.getIMEI(MainActivity.this);
                editer.putString(PreferencesActivity.PHONE_IMEI,mIMEI);
                editer.commit();
            }
            // 存储假数据库密码在xml文件
            if(prefs.getString(PreferencesActivity.DB_KEY,null)==null){
                editer.putString(PreferencesActivity.DB_KEY,GetMD5.getMD5(mIMEI));
                editer.commit();
            }

            editText_password.setHint("请创建您的密码：");
            button_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取输入的密码
                    String password = editText_password.getText().toString();
                    if("".equals(password)){
                        new AlertDialog.Builder(MainActivity.this).setTitle("密码不能为空！");
                    }else{
                        // 将密码存储入数据库
                        manager.addLoginPassword(GetMD5.getMD5(password));
                        // 标识已设置密码
                        editer.putBoolean(PreferencesActivity.FIRST_OPEN,false);
                        editer.commit();
                        gotoIndex(true);
                    }
                }
            });
        }else{
            button_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 获取输入的密码，并验证
                    String password = editText_password.getText().toString();
                    if("".equals(password)){
                        new AlertDialog.Builder(MainActivity.this).setTitle("密码不能为空！");
                    }else if(GetMD5.getMD5(password).equals(manager.getLoginPassword())){
                        gotoIndex(true);
                    }else{
                        gotoIndex(false);
                    }
                }
            });
        }
    }

    /**
     * 进入目录界面
     * @param value 标识状态信息
     */
    private void gotoIndex(boolean value) {
        Intent intent = new Intent(MainActivity.this, TrueActivity.class);
        intent.putExtra(PreferencesActivity.VERIFICATION_STATUS, value);
        startActivity(intent);
        finish();
    }

    /**
     * 初始化信息
     */
    private void initView(){
        textView_title = (TextView) findViewById(R.id.main_textView_title);
        editText_password = (EditText) findViewById(R.id.main_editText_password);
        button_confirm = (Button) findViewById(R.id.main_button_confirm);

        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        editer = prefs.edit();

        manager = new FakeManager(this);
    }
}
